Skip to main content

PositionSize

PositionSize declares how much capital to allocate to a symbol when an entry signal fires. Each entry sized this way is the baseline that ScalingRule scale_in_percentage values multiply against.

from investing_algorithm_framework import PositionSize

Signature

PositionSize(
symbol: str | None = None,
percentage_of_portfolio: float | None = None,
fixed_amount: float | None = None,
)
ParameterTypeDefaultDescription
symbolstr | NoneNoneTarget symbol (e.g. "BTC"). When None, this entry is a default used for any symbol that doesn't have its own symbol-specific PositionSize — a symbol-specific entry always takes precedence.
percentage_of_portfoliofloat | NoneNonePercent (0–100) of total portfolio value (unallocated + allocated) to spend per entry.
fixed_amountfloat | NoneNoneFixed amount in the trading currency to spend per entry. Overrides percentage_of_portfolio if both are given.

Exactly one of percentage_of_portfolio or fixed_amount must be supplied — otherwise the framework raises OperationalException.

How the size is computed

fixed_amount             → returns fixed_amount
percentage_of_portfolio → (unallocated + allocated) * pct / 100

Because the percentage is taken against total portfolio value (not just unallocated cash), the size is stable across the run as the portfolio grows. When several symbols all want capital at once and the request exceeds available funds, the framework scales every order down by the same ratio so allocation stays proportional.

Examples

Equal-weight portfolio

class EqualWeightStrategy(TradingStrategy):
symbols = ["BTC", "ETH", "SOL", "ADA", "XRP"]

position_sizes = [
PositionSize(symbol=s, percentage_of_portfolio=20.0)
for s in symbols
]

Default for all symbols, with a per-symbol override

class DefaultWithOverrideStrategy(TradingStrategy):
symbols = ["BTC", "ETH", "SOL"]

position_sizes = [
# Applies to every symbol above that doesn't have its own
# entry below.
PositionSize(percentage_of_portfolio=20.0),
# BTC gets its own size instead of the 20% default.
PositionSize(symbol="BTC", percentage_of_portfolio=50.0),
]

Fixed-amount per asset

class FixedSizeStrategy(TradingStrategy):
symbols = ["BTC", "ETH"]

position_sizes = [
PositionSize(symbol="BTC", fixed_amount=1_000.0),
PositionSize(symbol="ETH", fixed_amount=500.0),
]

Mixed sizing

position_sizes = [
PositionSize(symbol="BTC", percentage_of_portfolio=30.0),
PositionSize(symbol="ETH", fixed_amount=750.0),
]

Interaction With Other Rules

  • ScalingRulescale_in_percentage is expressed as a percentage of the original PositionSize, not of the current position value.
  • TradingCostfee_percentage and slippage_percentage are applied on top of the order created from the size; the framework does not pre-deduct fees from the size itself.
  • Proportional scaling — when concurrent buys exceed available cash, every order is reduced by the same factor so no symbol is starved.

See Also